package com.hsjk.szwj.module.smedia.service.mediaUpload;

import cn.hutool.core.util.ObjectUtil;
import com.aliyun.ice20201109.Client;
import com.aliyun.ice20201109.models.CreateUploadStreamRequest;
import com.aliyun.ice20201109.models.CreateUploadStreamResponse;
import com.aliyun.ice20201109.models.RegisterMediaStreamRequest;
import com.aliyun.ice20201109.models.RegisterMediaStreamResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import com.hsjk.szwj.framework.common.exception.ServiceException;
import com.hsjk.szwj.framework.common.exception.enums.GlobalErrorCodeConstants;
import com.hsjk.szwj.module.smedia.controller.admin.mediaUpload.vo.CreateUploadStreamReqVO;
import com.hsjk.szwj.module.smedia.controller.admin.mediaUpload.vo.CreateUploadStreamRespVO;
import com.hsjk.szwj.module.smedia.controller.admin.mediaUpload.vo.RegisterMediaStreamReqVO;
import com.hsjk.szwj.module.smedia.controller.admin.mediaUpload.vo.RegisterMediaStreamRespVO;
import com.hsjk.szwj.module.smedia.convert.mediaUpload.MediaUploadConvert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

/**
 * @author chenjiawei
 * @desc 媒资上传接口实现
 * @Date 2023/07/23 19:26
 **/
@Service
@Validated
@Slf4j
public class MediaUploadImpl implements IMediaUpload {
    @Value("${ali.media.access-key-id}")
    private String accessKeyId;

    @Value("${ali.media.access-key-secret}")
    private String accessKeySecret;

    @Value("${ali.media.endpoint}")
    private String endpoint;

    @Override
    public CreateUploadStreamRespVO createUploadStream(CreateUploadStreamReqVO reqVO) {
        Config config = new Config();
        config.setAccessKeyId(accessKeyId);
        config.setAccessKeySecret(accessKeySecret);
        config.setEndpoint(endpoint);
        CreateUploadStreamRespVO response = null;
        try {
            Client client = new Client(config);
            RuntimeOptions runtime = new RuntimeOptions();
            CreateUploadStreamRequest request = new CreateUploadStreamRequest();
            if (ObjectUtil.isNotEmpty(reqVO.getMediaId())) {
                request.setMediaId(reqVO.getMediaId());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getDefinition())) {
                request.setDefinition(reqVO.getDefinition());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getFileExtension())) {
                request.setFileExtension(reqVO.getFileExtension());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getHdrType())) {
                request.setHDRType(reqVO.getHdrType());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getUserData())) {
                request.setUserData(reqVO.getUserData());
            }
            CreateUploadStreamResponse withOptions = client.createUploadStreamWithOptions(request, runtime);
            if (ObjectUtil.isNotNull(withOptions)) {
                response = MediaUploadConvert.INSTANCE.convertCreateUploadStreamRespVO(withOptions.getBody());
            }
        } catch (TeaException teaException) {
            log.error(teaException.message);
        } catch (Exception exception) {
            throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(), exception.getMessage());
        }
        return response;
    }

    @Override
    public RegisterMediaStreamRespVO registerMediaStream(RegisterMediaStreamReqVO reqVO) {
        Config config = new Config();
        config.setAccessKeyId(accessKeyId);
        config.setAccessKeySecret(accessKeySecret);
        config.setEndpoint(endpoint);
        RegisterMediaStreamRespVO response = null;
        try {
            Client client = new Client(config);
            RuntimeOptions runtime = new RuntimeOptions();
            RegisterMediaStreamRequest request = new RegisterMediaStreamRequest();
            if (ObjectUtil.isNotEmpty(reqVO.getMediaId())) {
                request.setMediaId(reqVO.getMediaId());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getUserData())) {
                request.setUserData(reqVO.getUserData());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getInputUrl())) {
                request.setInputURL(reqVO.getInputUrl());
            }
            RegisterMediaStreamResponse mediaStreamResponse = client.registerMediaStreamWithOptions(request, runtime);
            if (ObjectUtil.isNotNull(mediaStreamResponse)) {
                response = MediaUploadConvert.INSTANCE.convertRegisterMediaStreamRespVO(mediaStreamResponse.getBody());
            }
        } catch (TeaException teaException) {
            log.error(teaException.message);
        } catch (Exception exception) {
            throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(), exception.getMessage());
        }
        return response;
    }
}
